#include "threadTask.h"



using namespace easyWebServer;


TDTask::TDTask()
{
    m_td = thread([this] {run();});
}

TDTask::~TDTask()
{

}

void TDTask::run()
{
    // sigset_t sets;
    // sigemptyset(&sets);
    // sigaddset(&sets, SIGINT);
    // sigprocmask(SIG_BLOCK, &sets, NULL);

    info("thead up pid = %d", gettid());
    while(1)
    {
        unique_lock<mutex> lk(Singleton<mutex>::getInstance());
        while(Singleton<TSList>::getInstance().size() == 0)
        {
            Singleton<condition_variable>::getInstance().wait(lk);
            if(m_isStop == true)
            {
                info("thread down pid = %d", gettid());
                lk.unlock();
                return;
            }
        }
        unique_ptr<Task> task = Singleton<TSList>::getInstance().front();
        Singleton<TSList>::getInstance().popFront();
        task->run();
        if(task->getStatue() == Task::Statue::TASK_IN)
        {
            Singleton<Epoll>::getInstance().delEvent(task->getFd());
            task = nullptr;
        }
        else if(task->getStatue() == Task::Statue::TASK_OUT)
        {
            Singleton<Epoll>::getInstance().delEvent(task->getFd());
            Singleton<Epoll>::getInstance().addEvent(task->getFd(), EPOLLOUT, task);
        }
        lk.unlock();
    }
}

void TDTask::stop()
{
    m_isStop = true;
}

void TDTask::join()
{
    m_td.join();
}




